package com.lei.day03lx;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.lei.day03lx.bean.StudentBean;
import com.lei.day03lx.db.DbHelper;
import com.lei.day03lx.db.DbManger;
import com.lei.day03lx.utlis.LogUtils;

import java.util.List;

@Route(path = "/day03lx/MainActivity")
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_name;
    private EditText et_pwd;
    private Button bt_insert;
    private Button bt_query;
    private EditText et_delete;
    private Button bt_delete;
    private EditText et_update;
    private Button bt_update;
    private long a = 0;
    private Button bt_arouter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DbManger.getInstance().initDb(this);
        ARouter.getInstance().inject(this);

        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        et_name = (EditText) findViewById(R.id.et_name);
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        bt_insert = (Button) findViewById(R.id.bt_insert);
        bt_query = (Button) findViewById(R.id.bt_query);
        et_delete = (EditText) findViewById(R.id.et_delete);
        bt_delete = (Button) findViewById(R.id.bt_delete);
        et_update = (EditText) findViewById(R.id.et_update);
        bt_update = (Button) findViewById(R.id.bt_update);

        bt_insert.setOnClickListener(this);
        bt_query.setOnClickListener(this);
        bt_delete.setOnClickListener(this);
        bt_update.setOnClickListener(this);
        bt_arouter = (Button) findViewById(R.id.bt_arouter);
        bt_arouter.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_insert:
                StudentBean studentBean = new StudentBean();
                String trim = et_name.getText().toString().trim();
                String trim1 = et_pwd.getText().toString().trim();
                studentBean.setName(trim);
                studentBean.setPwd(trim1);
                DbHelper.insert(studentBean);
                break;
            case R.id.bt_query:
                List<StudentBean> query = DbHelper.query();
                for (int i = 0; i < query.size(); i++) {
                    LogUtils.e("id:" + query.get(i).getId() + "_name:" + query.get(i).getName() + "_mark:" + query.get(i).getPwd());
                }
                break;
            case R.id.bt_delete:
                DbHelper.delete(et_delete.getText().toString().trim());
                break;
            case R.id.bt_update:
                DbHelper.update(et_update.getText().toString().trim());
                break;
            case R.id.bt_arouter:
                ARouter.getInstance().build("/day03rikao/ZMainActivity").withString("name","123").navigation();
                break;
        }
    }

    private void submit() {
        // validate
        String name = et_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "请输入姓名", Toast.LENGTH_SHORT).show();
            return;
        }

        String pwd = et_pwd.getText().toString().trim();
        if (TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }

        String delete = et_delete.getText().toString().trim();
        if (TextUtils.isEmpty(delete)) {
            Toast.makeText(this, "请输入要删除的id", Toast.LENGTH_SHORT).show();
            return;
        }

        String update = et_update.getText().toString().trim();
        if (TextUtils.isEmpty(update)) {
            Toast.makeText(this, "请输入要修改的id", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DbManger.getInstance().closedb();
    }
}
